package com.scnu.common.config.mvc;


import com.scnu.common.annotations.NoWrapper;
import com.scnu.common.result.Result;
import com.scnu.common.utils.WebUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@RestControllerAdvice
@ConditionalOnClass({ResponseBodyAdvice.class})
public class WrapperResponseBodyAdvice implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter methodParameter, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
        if(methodParameter.hasMethodAnnotation(NoWrapper.class)){
            // 如果方法上标注了NoWrapper注解，则不进行包装
            return false;

        }
        if(methodParameter.getParameterType() == Result.class){
            return false;
        }
//        return WebUtils.isGatewayForwardedRequest();
        return true;
    }

    @Override
    public Object beforeBodyWrite(
            Object body, @NonNull MethodParameter returnType, @NonNull MediaType selectedContentType,
            @NonNull Class<? extends HttpMessageConverter<?>> selectedConverterType,
            @NonNull ServerHttpRequest request, @NonNull ServerHttpResponse response) {
/*        if (request.getURI().getPath().equals("/v2/api-docs")){
            return body;
        }*/
        if (body == null) {
            return Result.success();
        }
        if(body instanceof Result){
            return body;
        }
        return Result.success(body);
    }
}
